package iscript.uiFramework.components.panel
{
	import flash.display.DisplayObjectContainer;
	import iscript.uiFramework.components.button.BaseButton;
	import iscript.uiFramework.components.button.ButtonGroup;
	import iscript.uiFramework.components.button.ButtonState;
	import iscript.uiFramework.components.button.IButton;
	import iscript.uiFramework.components.container.Box;
	import iscript.uiFramework.components.container.IBox;
	import iscript.uiFramework.components.container.ScrollBox;
	import iscript.uiFramework.components.layout.HBoxLayout;
	import iscript.uiFramework.components.layout.LayoutMargin;
	import iscript.uiFramework.components.UIComponent;
	
	public class TabPanel extends UIComponent 
	{
		private var _tabModels:Vector.<TabModel>;
		private var _hbox:IBox;
		private var _buttongGroup:ButtonGroup;
		private var _activeIndex:int;
		
		public function TabPanel(preferWidth:Number = 400, preferHeight:Number = 100)
		{
			_buttongGroup = new ButtonGroup();
			_tabModels = new Vector.<TabModel>();
			var hLayout:HBoxLayout = new HBoxLayout();
			hLayout.vgap = 0;
			hLayout.hgap = 0;
			hLayout.margin = LayoutMargin.Empty;
			_hbox = new Box(preferWidth,30);
			_hbox.setLayout(hLayout);
			_hbox.render(this);
			super(preferWidth, preferHeight);
		}
		
		public function addTab(name:String):void
		{
			var button:BaseButton = new BaseButton();
			button.height = 24;
			button.text = name;
			button.ClickHandler = function():void {
									activeTab(getTabIndex(button))
									};
			_hbox.append(button);
			var model:TabModel = new TabModel();
			model.button = button;
			model.body = new ScrollBox(_preferWidth, _preferHeight - button.height+1);
			model.body.y = button.height-1;
			model.body.border = true;
			model.body.getBorder().color = 0x44A9F4;
			model.body.visible = false;
			model.body.render(this);
			_tabModels.push(model);
			_buttongGroup.append(button);
		}
		
		
		public function activeTab(index:int):void
		{
			index  = checkTabIndex(index);
			for each (var tabModel:TabModel  in _tabModels) 
			{
				 tabModel.button.normal();
				 tabModel.body.visible = false;
			}
			UIComponent(_tabModels[index].body).visible = true;
			IButton(_tabModels[index].button).state = ButtonState.DOWN;
			_activeIndex = index;
		}
		
		protected function getTabIndex(but:IButton):int {
			   	for(var i:int = 0; i < _tabModels.length;i++ ) 
				{
					if (_tabModels[i].button == but) return i; 
			    }
			   return -1;
			}
		
		public function removeTabAt(index:int):void
		{
			index  = checkTabIndex(index);
			_buttongGroup.remove(_tabModels[index].button);
			_hbox.remove(_tabModels[index].button);
			removeChild(_tabModels[index].body);
			_tabModels.splice(index, 1);
		}
		
		public function getBody(index:int):ScrollBox
		{
			index  = checkTabIndex(index);
			return _tabModels[index].body;
		}
		
		protected function checkTabIndex(index:int):int {
			  if (index > _tabModels.length - 1) index = _tabModels.length - 1;
			  if (index < 0) index = 0;
			  return index;
			}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			activeTab(0);
			super.render(container);
		}
		
		override public function set width(value:Number):void
		{
			_preferWidth = value;
			_hbox.width = value;
			for each (var itemL:TabModel  in _tabModels) 
			{
				 itemL.body.width =  value;
			}
			super.width = value;
		}
		
		
		override public function set height(value:Number):void 
		{
			_preferHeight = value;
			for each (var itemL:TabModel  in _tabModels) 
			{
				 itemL.body.height =  _preferHeight - itemL.button.height+1;
			}
			super.height = value;
		}
		override public function get width():Number
		{
			return _preferWidth;
		}
		
		override public function get height():Number
		{
			return _preferHeight;
		}
		
		public function get activeIndex():int 
		{
			return _activeIndex;
		}
	}

}


import iscript.uiFramework.components.button.BaseButton;
import iscript.uiFramework.components.container.ScrollBox;

internal class TabModel
{
	public var button:BaseButton;
	public var body:ScrollBox;
}

